package com.yuexin.project.dailymenu.menumanager.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuexin.common.utils.ServletUtils;
import com.yuexin.common.utils.security.ShiroUtils;
import com.yuexin.framework.aspectj.lang.annotation.Log;
import com.yuexin.framework.aspectj.lang.enums.BusinessType;
import com.yuexin.framework.web.controller.BaseController;
import com.yuexin.framework.web.domain.AjaxResult;
import com.yuexin.framework.web.page.TableDataInfo;
import com.yuexin.project.dailymenu.dishlist.service.DishIListService;
import com.yuexin.project.dailymenu.menumanager.domain.Manager;
import com.yuexin.project.dailymenu.menumanager.service.IManagerService;

/**
 * 菜单管理列 信息操作处理
 * 
 * @author yuexin
 * @date 2018-10-10
 */
@Controller
@RequestMapping("/dailymenu/menumanager")
public class ManagerController extends BaseController
{
    private String prefix = "dailymenu/menumanager";
	
	@Autowired
	private IManagerService managerService;
	
	@Autowired
	private DishIListService listService;
	
	@RequiresPermissions("dailymenu:manager:view")
	@GetMapping()
	public String manager()
	{
	    return prefix + "/manager";
	}
	
	/**
	 * 查询菜单管理列列表
	 */
	@RequiresPermissions("dailymenu:manager:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Manager manager)
	{
		startPage();
		
		String menuName = ServletUtils.getParameter("searchValue");
		manager.setMenuName(menuName);
		manager.setCompanyId(ShiroUtils.getCompanyId());
		List<Manager> list = managerService.selectManagerList(manager);
        
        for(Manager mn : list){
			String isBreakFast = mn.getIsBreakFast();
			String isLunch = mn.getIsLunch();
			String isDinner = mn.getIsDinner();
			String isOther = mn.getIsOther();

			if("1".equals(isBreakFast)){
				mn.setDinnerType("0");
				if(getDishName(mn) == null ||"".equals(getDishName(mn))){
					mn.setIsBreakFast("点击选择菜品");
				}else{
					mn.setIsBreakFast(getDishName(mn));
				}
			}
			
			if("1".equals(isLunch)){
				mn.setDinnerType("1");
				if(getDishName(mn) == null ||"".equals(getDishName(mn))){
					mn.setIsLunch("点击选择菜品");
				}else{
					mn.setIsLunch(getDishName(mn));
				}
			}
			
			if("1".equals(isDinner)){
				mn.setDinnerType("2");
				if(getDishName(mn) == null ||"".equals(getDishName(mn))){
					mn.setIsDinner("点击选择菜品");
				}else{
					mn.setIsDinner(getDishName(mn));
				}
			}
			if("1".equals(isOther)){
				mn.setDinnerType("3");
				if(getDishName(mn) == null ||"".equals(getDishName(mn))){
					mn.setIsOther("点击选择菜品");
				}else{
					mn.setIsOther(getDishName(mn));
				}
			}
		}

		return getDataTable(list);
	}
	
	private String getDishName(Manager manager){
		String dishNames = "";
		List<String> nameList = listService.selectMenuDishList(manager);
		if(nameList != null && nameList.size()>0){
			for(String name : nameList){
				dishNames += name;
				dishNames = dishNames+"、";
			}
		}
		
		if(!"".equals(dishNames) && dishNames != null){
			dishNames = dishNames.substring(0,dishNames.length()-1);
		}
		return dishNames;
	}
	
	/**
	 * 新增菜单管理列
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存菜单管理列
	 */
	@RequiresPermissions("dailymenu:manager:add")
	@Log(title = "菜单管理列", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Manager manager)
	{		
		manager.setCompanyId(ShiroUtils.getCompanyId());
		manager.setMenuType("0");
		manager.setIsBreakFast("1");
		manager.setIsLunch("1");
		manager.setIsDinner("1");
		return toAjax(managerService.insertManager(manager));
	}

	/**
	 * 修改菜单管理列
	 */
	@GetMapping("/edit/{menuId}")
	public String edit(@PathVariable("menuId") Integer menuId, ModelMap mmap)
	{
		Manager manager = managerService.selectManagerById(menuId);
		mmap.put("manager", manager);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存菜单管理列
	 */
	@RequiresPermissions("dailymenu:manager:edit")
	@Log(title = "菜单管理列", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Manager manager)
	{		
		manager.setMenuType("0");
		return toAjax(managerService.updateManager(manager));
	}
	
	/**
	 * 删除菜单管理列
	 */
	@RequiresPermissions("dailymenu:manager:remove")
	@Log(title = "菜单管理列", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(managerService.deleteManagerByIds(ids));
	}
	
	 /**
     * 新增菜单菜品
     */
    @GetMapping("/dish/{menuId}/{dinnerType}")
    public String dish(@PathVariable("menuId") Integer menuId,@PathVariable("dinnerType") String dinnerType, ModelMap mmap)
    {
    	//mmap.put("dinnerType", dinnerType);
    	
    	Manager manger = managerService.selectManagerById(menuId);
    	manger.setCompanyId(ShiroUtils.getCompanyId());
    	String isOpen = "0";
    	if( "0".equals(dinnerType)){
    		if("1".equals(manger.getIsBreakFast())){
    			isOpen = "1";
        	}
    	}else if("1".equals(dinnerType)){
    		if("1".equals(manger.getIsLunch())){
    			isOpen = "1";
        	}
    	}else if("2".equals(dinnerType)){
    		if("1".equals(manger.getIsDinner())){
    			isOpen = "1";
        	}
    	}else if("3".equals(dinnerType)){
			if("1".equals(manger.getIsOther())){
				isOpen = "1";
			}
		}
    	
    	manger.setIsOpen(isOpen);
    	manger.setDinnerType(dinnerType);
    	mmap.put("menu", manger);
    	
        return prefix + "/dish";
    }
    
    /**
     * 修改保存数据权限
     */
    @RequiresPermissions("dailymenu:manager:edit")
    @Log(title = "菜单管理管理", businessType = BusinessType.UPDATE)
    @PostMapping("/rule")
    @ResponseBody
    public AjaxResult ruleSave(Manager manager)
    {
        return toAjax(managerService.updateRule(manager));
    }
	
}
